package com.yksj.healthtalk.ui.doctorstation;

import org.handmark.pulltorefresh.library.PullToRefreshBase;
import org.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import org.handmark.pulltorefresh.library.PullToRefreshListView;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.yksj.healthtalk.adapter.DoctorStationFriendListAdapter;
import com.yksj.healthtalk.comm.BaseFragmentActivity;
import com.yksj.healthtalk.services.MessagePushService;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.ui.app.AppData;
import com.yksj.healthtalk.ui.app.HTalkApplication;
import com.yksj.healthtalk.utils.FriendHttpUtil;


/**
 * 
 * 患者列表
 * @author zhao
 *
 */
public class DoctorStationFriendListActivity extends BaseFragmentActivity implements OnClickListener, OnRefreshListener2<ListView> , OnItemClickListener{
	DoctorStationFriendListAdapter mAdapter;
	PullToRefreshListView mPullToRefreshListView;
	BroadcastReceiver receiver = new BroadcastReceiver(){
		@Override
		public void onReceive(Context context, Intent intent) {
			String action = intent.getAction();
			if (action.equals(MessagePushService.ACTION_MESSAGE)) {
				if (intent.hasExtra("senderId")) {
					mAdapter.notifyDataSetChanged();
					}}
		}
	};
	
	
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.doctorstation_list_layout);
		initTitle();
		titleTextV.setText("患者名录");
		titleLeftBtn.setOnClickListener(this);
		mPullToRefreshListView = (PullToRefreshListView)findViewById(R.id.pull_refresh_listview);
		mPullToRefreshListView.setOnItemClickListener(this);
		AppData appData = HTalkApplication.getAppData();
		mPullToRefreshListView.setAdapter(mAdapter = new DoctorStationFriendListAdapter(appData.getCustomerInfoList(),getLayoutInflater(),this));
		mPullToRefreshListView.getRefreshableView().addFooterView(LayoutInflater.from(this).inflate(R.layout.list_line_layout, null));
		mPullToRefreshListView.setOnRefreshListener(this);
	}
	
	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.title_back:
			onBackPressed();
			break;
		}
	}
	
	@Override
	public void onStart() {
		IntentFilter filter = new IntentFilter();
		filter.addAction(MessagePushService.ACTION_MESSAGE);
		registerReceiver(receiver, filter);
		super.onStart();
	}
	
	@Override
	public void onStop() {
		unregisterReceiver(receiver);
		super.onStop();
	}
	
	@Override
	protected void onActivityResult(int arg0, int arg1, Intent arg2) {
		super.onActivityResult(arg0, arg1, arg2);
		notifyMeoth();
	}

	@Override
	protected void onResume() {
		super.onResume();
		notifyMeoth();
	}
	
	private void notifyMeoth(){
		mAdapter.mInfoEntities.clear();
		mAdapter.mInfoEntities.addAll(HTalkApplication.getAppData().getCustomerInfoList());
		if(mAdapter.mInfoEntities.size()<0)
		mPullToRefreshListView.getRefreshableView().removeAllViews();
		mAdapter.notifyDataSetChanged();
	}

	@Override
	public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
		mPullToRefreshListView.onRefreshComplete();
		notifyMeoth();
	}

	@Override
	public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
		mPullToRefreshListView.onRefreshComplete();
		notifyMeoth();
	}

	
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		position--;
		//判断是否收费
		FriendHttpUtil.onItemClick(this, mAdapter.getItem(position));
		
	}
}
